package cn.qiandn.util;

public class Actions {
	//public final static String ACTION="action";
	public final static String VIEW_PATTERN="/";
	
	public final static String PAGE_PATH="/WEB-INF/jsp/";
	public final static String SUFFIX=".jsp";
	
	public final static String LIST="list";
	public final static String SHOW="show";
	
	public final static String ADD="add";
	public final static String ADD_SAVE="addSave";
	
	public final static String EDIT="edit";
	public final static String EDIT_SAVE="editSave";
	
	public final static String DELETE="delete";
	
	public final static String ERROR="error";
	public final static String FAILURE="failure";
	public final static String DENIED="denied";
	public final static String SUCCESS="success";
	
	public final static String SEARCH="search";
	
	public final static String REDIRECT="redirect:";
	
	
	public static final String LOGIN_SUECCESS_URL="/user/list";
	public static final String LOGIN_DEFUALT_URL="/user/login";
	public static final String LOGIN_FAILURE_URL="/user/login";
	public static final String AUTHENTICAION_FAILURE_URL="/message/denied";
	
	
	
	public static String REDIRECT(Class clazz,String method){
		return REDIRECT+"/"+controllerName(clazz)+"/"+method;
	}
	public static String REDIRECT(String controller,String method){
		return REDIRECT+"/"+controller+"/"+method;
	}
	public static String REDIRECT(String uri){
		return REDIRECT+uri;
	}
	public static String DENIED(Class clazz){
		return getDirectory(clazz)+VIEW_PATTERN+getPage(DENIED);
	}
	public static String ADD(Class clazz){
		return getDirectory(clazz)+VIEW_PATTERN+getPage(ADD);
	}
	public static String LIST(Class clazz){
		return getDirectory(clazz)+VIEW_PATTERN+getPage(LIST);
	}
	public static String EDIT(Class clazz){
		return getDirectory(clazz)+VIEW_PATTERN+getPage(EDIT);
	}
	public static String SHOW(Class clazz){
		return getDirectory(clazz)+VIEW_PATTERN+getPage(SHOW);
	}
	public static String SUCCESS(Class clazz){
		return getDirectory(clazz)+VIEW_PATTERN+getPage(SUCCESS);
	}
	public static String SEARCH(Class clazz){
		return getDirectory(clazz)+VIEW_PATTERN+getPage(SEARCH);
	}
	public static String ERROR(Class clazz){
		return getDirectory(clazz)+VIEW_PATTERN+getPage(ERROR);
	}
	public static String FAILURE(Class clazz){
		return getDirectory(clazz)+VIEW_PATTERN+getPage(FAILURE);
	}
	public static String METHOD(Class clazz,String method){
		return getDirectory(clazz)+VIEW_PATTERN+getPage(method);
	}
	
	public static String METHOD(String  controller,String method){
		return PAGE_PATH+controller+VIEW_PATTERN+getPage(method);
	}
	
	private static String controllerName(Class clazz){
		String controller="Controller";
		String className=clazz.getSimpleName();
		String action=clazz.getSimpleName();
		if(className.contains(controller))
			action=className.substring(0,className.indexOf(controller));
		return action.substring(0, 1).toLowerCase()+action.substring(1, action.length());
		
	}
	public static String EORROR(Class clazz){
		return controllerName(clazz)+VIEW_PATTERN+getPage(ERROR); 
	}
	public static String PagePath(String controllerAndMethod){
		return PAGE_PATH+controllerAndMethod+SUFFIX;
	}
	
	private static String getDirectory(Class clazz){
		return PAGE_PATH+controllerName(clazz);
	}
	private static String getPage(String method){
		return method+SUFFIX;
	}
}
